//
//  ViewController.swift
//  ActiveLabelDemo
//
//  Created by 李煜 on 2024/5/17.
//

import ActiveLabel
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let label = ActiveLabel()
        label.numberOfLines = 0
        label.enabledTypes = [.mention, .hashtag, .url]
        label.text = "This is a post with #hashtags and a @userhandle."
        label.textColor = .black
        label.handleHashtagTap { hashtag in
            print("Success. You just tapped the \(hashtag) hashtag")
        }

        let customType = ActiveType.custom(pattern: "\\swith\\b") // Regex that looks for "with"
        label.enabledTypes = [.mention, .hashtag, .url, customType]
        label.text = "This is a post with #hashtags and a @userhandle."
        label.customColor[customType] = UIColor.purple
        label.customSelectedColor[customType] = UIColor.green
        label.handleCustomTap(for: customType) { element in
            print("Custom type tapped: \(element)")
        }
        view.addSubview(label)
        label.frame = CGRect(x: 10, y: 100, width: 200, height: 35)
        label.sizeToFit()

        let demoLabel = ActiveLabel(frame: CGRectMake(10, 300, 200, 40))
        demoLabel.numberOfLines = 0
        view.addSubview(demoLabel)
        let type = ActiveType.custom(pattern: "\\《(服务协议|隐私协议)\\》")
        demoLabel.enabledTypes = [type]
        let text = """
        请您务必审慎阅读、充分理解“服务协议”和“隐私协议”各条款，包括但不限于：为    了更好的向您提供服务，我们需要收集您的设备标识、操作日志等信息用于分析、优化应用性能。 \n您可阅读《服务协议》和《隐私协议》了解详细信息。如果你同意，请点击下面按钮开始接受我们的服务。
        """
        
        demoLabel.text  = text
        demoLabel.customColor[type] = .orange
        demoLabel.customSelectedColor[type] = .black
        demoLabel.handleCustomTap(for: type) { ele in
            print("点击了元素: \(ele)")
        }
        demoLabel.sizeToFit()

        var p: NSMutableParagraphStyle = NSMutableParagraphStyle()
        p.alignment = .left
        
        let font14 = UIFont.systemFont(ofSize: 14)
        p.firstLineHeadIndent = font14.pointSize * 2
        p.lineSpacing = 2.0
        let attDic: [NSAttributedString.Key: Any] = [.paragraphStyle: p,
                                                     .kern: 1,
                                                     .font: font14]
        let att = NSMutableAttributedString(string: text)
        let r3 = NSMakeRange(0, att.length)
        
        att.addAttributes(attDic, range: r3)
        demoLabel.attributedText = att
    }
}
